/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.proxy;

/**
 * @author <a href="mailto:bluesotne.master@gmail.com">daniel.q</a>
 *
 */
public class AJAXUtil {
	
	public static final String CALL_NOTHING  ="[001]";
	public static final String CALL_FUNCTION ="[002]";
	public static final String CALL_SUCCESS = "[003]";

		
	   /**
	    * @param value, variable value used in Javascript
	    * @return escaped variable value
	    */
	   public static String JavascriptEscape(String value)
	   {
	      // back slash(\) is special character, need to be encoded as (\\)
	      // double quote(") is special character, need to be encoded as (\")
	      // single quote(') need to be encoded as (\')
	      char[] specialChars = {'\\', '\"', '\'','\n','\t','\r'};
	      String[] speCharEscaped = {"\\\\", "\\\"", "\\'","\\n","\\t","\\r"};
	      return replace( value, specialChars, speCharEscaped );
	   }
	   
	   public static String replace( String origString, char from, String to )
	   {
	      if ( origString == null || origString.indexOf(from) < 0 )
	      {
	         // no replace happens
	         return origString;
	      }
	      else
	      {
	         // replace happens
	         char [] tempCharArray = origString.toCharArray();
	         int len = tempCharArray.length;
	         StringBuffer tempStrBuf = new StringBuffer(len<<1);   // double capacity

	         for ( int i = 0; i < len; i++ )
	         {
	            if ( tempCharArray[i] == from )
	            {
	               tempStrBuf.append(to);
	            }
	            else
	            {
	               tempStrBuf.append(tempCharArray[i]);
	            }
	         }
	         return (tempStrBuf.toString());
	      }
	   }


	   public static String replace( String origString, char[] froms, String[] tos )
	   {
	      if ( origString == null ||
	           froms.length != tos.length )
	      {
	         return origString;
	      }
	      else
	      {
	         int len = origString.length();
	         StringBuffer tempStrBuf = new StringBuffer(len<<1);
	         char tempChar;
	         int j;
	         int mapLength = froms.length;
	         for ( int i = 0; i < len; i++ )
	         {
	            tempChar = origString.charAt(i);
	            for ( j = 0; j < mapLength; j++ )
	            {
	               if ( tempChar == froms[j] )
	               {
	                  tempStrBuf.append(tos[j]);
	                  break;
	               }
	            }
	            if ( j == mapLength )
	            {
	               tempStrBuf.append(tempChar);
	            }
	         }
	         return (new String(tempStrBuf));
	      }
	   }	   

}
